package small.common.core.util;

import small.common.core.collection.CollectionUtil;

import java.util.Collection;
import java.util.List;

/**
 * @Author zhoujin
 * @Date 2022/11/22 19:00
 */
public class ParamUtil {

    private static char[] validChars = new char[] {'_', '-', '.', ':'};

    private static final int TAG_MAX_LEN = 16;

    private static final int MAX_LEN = 128;


    /**
     * 检查参数
     * @param tag
     */
    public static void checkParam(String tag, int length) {
        if (StringUtil.isNotBlank(tag)) {
            if (!isValid(tag.trim())) {
                throw new IllegalArgumentException("invalid tag : " + tag);
            }
            if (tag.length() > length) {
                throw new IllegalArgumentException("too long tag, over "+length);
            }
        }
    }
    /**
     * 检查参数
     * @param list
     */
    public static void checkParam(List<String> list) {
        if (CollectionUtil.isNotEmpty(list)){
            for (String tag : list) {
                checkParam(tag,MAX_LEN);
            }
        }
    }
    /**
     * 检查参数
     * @param tag
     */
    public static void checkParam(String tag) {
        checkParam(tag,MAX_LEN);
    }

    /**
     * 判断字段传是否有效
     * @param param
     * @return
     */
    public static boolean isValid(String param) {
        if (param == null) {
            return false;
        }
        int length = param.length();
        for (int i = 0; i < length; i++) {
            char ch = param.charAt(i);
            if (!Character.isLetterOrDigit(ch) && !isValidChar(ch)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否是有效字符
     * @param ch
     * @return
     */
    private static boolean isValidChar(char ch) {
        for (char c : validChars) {
            if (c == ch) {
                return true;
            }
        }
        return false;
    }

    /**
     * 参数不为空
     * @param string
     * @param message
     */
    public static void notEmpty(String string, String message) {
        if (StringUtil.isEmpty(string)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 集合不为空
     * @param collection
     * @param message
     */
    public static void assertNotEmpty(Collection<?> collection, String message) {
        if (collection == null || collection.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 参数不含空白
     * @param string
     * @param message
     */
    public static void assertNotBlank(String string, String message) {
        if (StringUtil.isBlank(string)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 参数不为null
     * @param object
     * @param message
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 判断是否为真
     * @param value
     * @param message
     */
    public static void isTrue(boolean value, String message) {
        if (!value) {
            throw new IllegalArgumentException(message);
        }
    }
}
